package io.dshubs.archives.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.dshubs.archives.dao.ArchiveAppraisalMapper;
import io.dshubs.archives.dao.ArchiveBaseClassMapper;
import io.dshubs.archives.entity.ArchiveAppraisal;
import io.dshubs.archives.entity.ArchiveBaseCategory;
import io.dshubs.archives.entity.ArchiveBaseInfo;
import io.dshubs.archives.entity.ArchiveTransferList;
import io.dshubs.archives.service.ArchiveAppraisalService;
import io.dshubs.archives.service.ArchiveBaseCategoryService;
import io.dshubs.commons.mybatis.enums.DelFlagEnum;
import io.dshubs.commons.mybatis.service.impl.BaseServiceImpl;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.commons.security.user.UserDetail;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.enums.SuperAdminEnum;
import io.dshubs.commons.tools.exception.RenException;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.dto.SysDeptDTO;
import io.dshubs.dto.SysTenantDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案鉴定表 服务实现类
 * </p>
 *
 * @author tan
 * @since 2021-08-27
 */
@Service
public class ArchiveAppraisalServiceImpl extends BaseServiceImpl<ArchiveAppraisalMapper, ArchiveAppraisal> implements ArchiveAppraisalService {

    @Autowired
    private ArchiveBaseClassMapper archiveBaseClassMapper;

    @Autowired
    private ArchiveBaseCategoryService archiveBaseCategoryService;

    @Override
    public PageData<ArchiveAppraisal> page(Map<String, Object> params) {
        IPage<ArchiveAppraisal> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        PageData<ArchiveAppraisal> pageData = getPageData(page, ArchiveAppraisal.class);
        List<ArchiveAppraisal> list = pageData.getList();
        if(list.size() > 0) {
            list.forEach(item->{
                ArchiveBaseCategory archiveBaseCategory = archiveBaseClassMapper.selectById(item.getCategoryId());
                item.setCategoryName(archiveBaseCategory == null ? null : archiveBaseCategory.getName());
            });
        }
        return pageData;
    }

    private QueryWrapper<ArchiveAppraisal> getWrapper(Map<String, Object> params){
        List<Long> ids = archiveBaseCategoryService.getArchiveBaseCategoryId();
        String appraisalTitle = (String)params.get("appraisalTitle");
        String fileNumber = (String)params.get("fileNumber");
        String categoryId = (String)params.get("categoryId");
        String appraisalPerson = (String)params.get("appraisalPerson");
        String appraisalDateBegin = (String)params.get("appraisalDateBegin");
        String appraisalDateEnd = (String)params.get("appraisalDateEnd");
        QueryWrapper<ArchiveAppraisal> wrapper = new QueryWrapper<>();
        wrapper.eq(Constant.DEL_FLAG, DelFlagEnum.NORMAL.value());
        wrapper.ne("state",3);
        wrapper.like(StringUtils.isNotBlank(appraisalTitle), "appraisal_title", appraisalTitle);
        wrapper.eq(StringUtils.isNotBlank(fileNumber),"file_number",fileNumber);
        wrapper.like(StringUtils.isNotBlank(appraisalPerson),"appraisal_person",appraisalPerson);
        wrapper.ge(StringUtils.isNotBlank(appraisalDateBegin),"appraisal_date",appraisalDateBegin);
        wrapper.le(StringUtils.isNotBlank(appraisalDateEnd),"appraisal_date",appraisalDateEnd);
        if(StringUtils.isEmpty(categoryId)) {
            wrapper.in(CollectionUtils.isNotEmpty(ids),"category_id",ids);
        } else {
            wrapper.eq(StringUtils.isNotEmpty(categoryId),"category_id",categoryId);
        }
        UserDetail user = SecurityUser.getUser();
        if(user.getSuperAdmin() != SuperAdminEnum.YES.value()) {
            wrapper.eq("tenant_code",user.getTenantCode());
        }
        return wrapper;
    }

    @Override
    public void restore(List<String> ids) {
        if(CollectionUtils.isEmpty(ids)) {
            throw new RenException("id不能为空");
        }
        List<ArchiveAppraisal> archiveAppraisals = this.baseDao.selectBatchIds(ids);
        archiveAppraisals.forEach(item->{
            item.setState("3");
            this.baseDao.updateById(item);
        });
    }
}
